function processFile(blob, fileName) { var reader = new FileReader(); reader.onload = function (e) { var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); audioCtx.decodeAudioData(e.target.result, function (audioBuffer) { var numChannels = audioBuffer.numberOfChannels; var sampleRate = audioBuffer.sampleRate; var numSamples = audioBuffer.length; var dataSize = numSamples * numChannels * 2; var buf = new ArrayBuffer(44 + dataSize); var view = new DataView(buf); var ws = function (off, str) { for (var i = 0; i < str.length; i++) view.setUint8(off + i, str.charCodeAt(i)); }; ws(0, 'RIFF'); view.setUint32(4, 36 + dataSize, true); ws(8, 'WAVE'); ws(12, 'fmt '); view.setUint32(16, 16, true); view.setUint16(20, 1, true); view.setUint16(22, numChannels, true); view.setUint32(24, sampleRate, true); view.setUint32(28, sampleRate * numChannels * 2, true); view.setUint16(32, numChannels * 2, true); view.setUint16(34, 16, true); ws(36, 'data'); view.setUint32(40, dataSize, true); var offset = 44; for (var i = 0; i < numSamples; i++) { for (var ch = 0; ch < numChannels; ch++) { var s = audioBuffer.getChannelData(ch)[i]; s = Math.max(-1, Math.min(1, s)); view.setInt16(offset, s < 0 ? s * 32768 : s * 32767, true); offset += 2; } } var wavBlob = new Blob([buf], { type: 'audio/wav' }); add_file_output(URL.createObjectURL(wavBlob), fileName.replace(/\.mp3$/i, '.wav')); }, function () { alert('Could not decode the MP3 file. Please ensure it is a valid MP3 audio file.'); }); }; reader.readAsArrayBuffer(blob); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }